home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / pc / files / t_unix / bs941029.tgz / bbsx-941029.tar / bbsx / rerase.c < prev    next >
C/C++ Source or Header  |  1994-10-29  |  4KB  |  181 lines

  1. #ifndef __lint
  2. static char rcsid[] = "@(#) $Header: /home/dg1rtf/tcp/bbsx/RCS/rerase.c,v 1.1 1994/06/01 22:21:32 dg1rtf Exp $";
  3. #endif
  4.  
  5. #define _HPUX_SOURCE
  6.  
  7. #include <stdio.h>
  8.  
  9. #include <ctype.h>
  10. #include <dirent.h>
  11. #include <errno.h>
  12. #include <fcntl.h>
  13. #include <pwd.h>
  14. #include <signal.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <sys/socket.h>
  18. #include <sys/stat.h>
  19. #include <sys/time.h>
  20. #include <sys/wait.h>
  21. #include <termios.h>
  22. #include <unistd.h>
  23.  
  24.  
  25. #include "bbs.h"
  26. #include "bbs.hd"
  27.  
  28. typedef struct BID_ENT {
  29.     int mesg ;
  30.     char bid[LEN_BID+1];
  31.     short del;
  32.     struct BID_ENT *next;
  33. } BID_ENT;
  34.  
  35. extern optind;
  36. extern char *optarg;
  37. int err_flag;
  38. const struct cmdtable cmdtable[1];
  39.  
  40.  
  41. /* In rerase.c: */
  42. int main(int argc, char **argv);
  43.  
  44.  
  45. /*---------------------------------------------------------------------------*/
  46.  
  47.  
  48.  
  49. int main(int argc, char **argv)
  50. {
  51.   short  delete = 1;   /* msgs werden geloescht */
  52.   short  time_st = 1;  /* Time_stamp wird geaendert */
  53.   short  debug = 0;    /* Debugging ist OFF */
  54.   char     to[LEN_TO+1];
  55.   int    c;
  56.   struct index index;
  57.   struct BID_ENT *bid_ent, first;
  58.   struct passwd *pw;
  59.  
  60.   if (getuid()) {
  61.     pw = getpwnam(bbsadm);
  62.     if(getuid() != pw->pw_uid) {
  63.       perror("permission denied");
  64.       return 1;
  65.     }
  66.   }    
  67.  
  68.   if (chdir(WRKDIR)) {
  69.     mkdir(WRKDIR, 0755);
  70.     if(chdir(WRKDIR)) halt();
  71.   }  
  72.  
  73.   read_config();
  74.  
  75.  
  76.   bid_ent = &first;
  77.   bid_ent->next = (BID_ENT *) NULL;
  78.   
  79.   while ((c = getopt(argc, argv, "r:abdrt")) != EOF)
  80.     switch (c) {
  81.     case 'd':
  82.       debug = 1;
  83.       break;
  84.     case 'a':
  85.       strcpy(to,"A");
  86.       delete = 0;
  87.       break;
  88.     case 'b':
  89.       strcpy(to,"B");
  90.       delete = 0;
  91.       break;     
  92.     case 'r':
  93.       strcpy(to,strupc(optarg));
  94.       delete = 0;
  95.       break;
  96.     case 't':
  97.       time_st = 0;
  98.       break;  
  99.     case '?':
  100.       err_flag = 1;
  101.       break;
  102.     }
  103.   if (optind < argc) err_flag = 1;
  104.   if (err_flag) {
  105.     puts("usage: rerase [-d] [-a|b|r dest] [-t]");
  106.     exit(1);
  107.   }
  108.   
  109.   if ((fdindex = open(INDEXFILE, O_RDWR, 0644)) < 0) return 1;
  110.   
  111.  
  112.   for (; ; ) { 
  113.     if (read(fdindex, (char *) &index, sizeof(index)) != sizeof(index)) goto stop_look;
  114.     if (!(index.flags & DELETED) && index.to[1]) {
  115.       bid_ent->mesg=index.mesg;
  116.       strcpy(bid_ent->bid,index.bid);
  117.       bid_ent->del=0;
  118.       if ((bid_ent->next = malloc(sizeof(BID_ENT))) == NULL) {
  119.         puts("not enought memory");
  120.         return 1;
  121.       }  
  122.       bid_ent = bid_ent->next;
  123.       bid_ent->next = (BID_ENT *) NULL;
  124.     }  
  125.   }
  126. stop_look:
  127.  
  128.   if (lseek(fdindex, 0L, SEEK_SET))
  129.     return 1;
  130.     
  131.   for (; ; ) {
  132.     if (read(fdindex, (char *) &index, sizeof(index)) != sizeof(index)) goto stop_read;
  133.     if (!(index.flags & DELETED )) {
  134.       if (*index.to == 'E' && index.to[1] == 0 && !strcmp(index.at,"THEBOX")) {
  135.         bid_ent=&first;
  136.         while(bid_ent->next) {
  137.           if(!strcmp(bid_ent->bid,index.subject))
  138.             bid_ent->del=1;
  139.           bid_ent=bid_ent->next;
  140.         }
  141.       }
  142.     }  
  143.   }
  144. stop_read:
  145.  
  146.   bid_ent=&first;
  147.   while(bid_ent->next) {
  148.     if(bid_ent->del) {
  149.       if (!get_index(bid_ent->mesg, &index)) {
  150.         if (debug)
  151.           printf("Error in indexfile: msg %d\n",bid_ent->mesg);
  152.         return 1;
  153.       }  
  154.       if (time_st) 
  155.         index.date=time((long *) 0);
  156.       if (delete) {
  157.         unlink(getfilename(bid_ent->mesg));
  158.         index.flags |= DELETED;
  159.       }  
  160.       else {       
  161.         strcpy(index.to,to);
  162.         strcpy(index.at,Myhostname);
  163.       }  
  164.       if (debug) {
  165.         printf("msg: %d\t%s\t\tact. area: %s\tBID: %s\n",index.mesg,
  166.                 index.flags & DELETED ? "deleted" : "moved",
  167.                 index.to,index.bid);
  168.       }
  169.       if (lseek(fdindex, -sizeof(struct index), SEEK_CUR) < 0) return -1;
  170.       if (write(fdindex, &index, sizeof(struct index)) != sizeof(struct index))
  171.             return -1;
  172.     }                                
  173.     bid_ent=bid_ent->next;
  174.   }       
  175.   bid_ent=&first;
  176.   free(bid_ent->next);
  177.   close(fdindex);
  178.   
  179.   return 0;
  180. }
  181.